<?php
$sort = $this->sort;
$sort_name = array("class"=>"","rel"=>"asc");
$sort_date = array("class"=>"","rel"=>"asc");
$sort_status = array("class"=>"","rel"=>"asc");
$sort_local = array("class"=>"","rel"=>"asc");

switch($sort['by']){
    case "article_name": 
        $sort_name = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "article_date": 
       $sort_date = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "article_status":
        $sort_status = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "article_local": 
        $sort_local = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
}
?>
<table class="data display datatable">
    <thead>
        <tr>
            <th width="40" class="center">STT</th>
            <th class="sorting<?=$sort_name['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"list","sortby"=>"article_name"))?>" rel="<?=$sort_name["rel"]?>">Tên "<?=stripcslashes($this->cat->category_name)?>"</a>
            </th>
            <th width="100" class="sorting<?=$sort_date['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"list","sortby"=>"article_date"))?>" rel="<?=$sort_date['rel']?>">Ngày đăng</a>
            </th>
            <?php if($this->cat->has_album):?>
            <th width="60">Hình Ảnh</th>
            <?php endif;?>
            <?php if($this->cat->choice_once || $this->cat->choice_multi):?>
            <th width="70" class="sorting<?=$sort_status['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"list","sortby"=>"article_status"))?>" rel="<?=$sort_status['rel']?>">Hiển thị</a>
            </th>
            <?php endif;?>
            <?php if($this->cat->allow_update):?>
            <th width="60" class="center">Sửa</th>
            <?php endif;?>
            <?php if($this->cat->allow_del):?>
            <th width="50" class="center">Xóa</th>
            <?php endif;?>
            <th width="60" class="sorting<?=$sort_local['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"list","sortby"=>"article_local"))?>" rel="<?=$sort_local['rel']?>">local</a>
            </th>
        </tr>                   
    </thead>
    <tbody>
    <?php $stt=1; foreach($this->datas as $row){ $parity = ($stt%2)?"odd":"even";?>
        <tr class="gradeA <?=$parity?>">
            <td><?=$stt?></td>
            <td><?=stripcslashes($row->article_name)?></td>
            <?php
            $date = new Zend_Date($row->article_date);
            $article_date = $date->toString("dd-MM-Y");
            ?>
            <td><?=$article_date?></td>
            <?php if($this->cat->has_album){?>
            <td class="center">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"images","idart"=>$row->idarticles))?>" class="action view-album">[Xem]</a>
            </td>
            <?php } if($this->cat->choice_once){?>
            <td class="center">
                <a class="choice-once" rel="<?=$row->article_status?>" href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"index","choice"=>"once","id"=>$row->idarticles))?>">
                    <?php $ico = ($row->article_status)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
            </td>
            <?php }else if($this->cat->choice_multi){?>
            <td class="center">
                <a class="choice-multi" rel="<?=$row->article_status?>" href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"index","choice"=>"multi","id"=>$row->idarticles))?>">
                    <?php $ico = ($row->article_status)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
            </td>
            <?php }if($this->cat->allow_update){?>
            <td class="center">
                <a class="edit" href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"edit","idart"=>$row->idarticles))?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/icon-edit.png")?>" alt="[Sửa]" title="Sửa"/>
                </a>
            </td>
            <?php }if($this->cat->allow_del){?>
            <td class="center">
                <a class="del" href="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"del","id"=>$row->idarticles))?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/icon-del.png")?>" alt="[Xóa]" title="Xóa"/>
                </a>
            </td>
            <?php }?>
            <td class="center">
                <input rel="<?=$this->url(array("module"=>"admin","controller"=>"article","action"=>"index","do"=>"local","id"=>$row->idarticles))?>" type="text" size="3" value="<?=$row->article_local?>" class="change-local" />
            </td>
        </tr>
    <?php $stt++;}?>
    </tbody>
</table>
<?php if($this->datas->count()>1){// Nhiều hơn 1 trang thì sử dụng phân trang?>
<div class="dataTables_paginate paging_full_numbers">
    <?=$this->paginationControl($this->datas,"Sliding","partial/pagination-control.phtml")?>
</div>
<?php }?>
<div class="dialog-message" title="Thông báo" style="display: none;"></div>
<script>
$(function(){
    /*Chọn hiển thị một*/
    <?php if($this->cat->choice_once){?>
    $(".choice-once").click(function(){
        var icon = $(this).children("img").attr("src");
        var status = $(this).attr("rel");
        var link = $(this).attr("href");
        if(status==0){            
            $(".choice-once").attr("rel",0);
            $(".choice-once").children("img").attr("src",icon);
            $(this).attr("rel",1);
            n_icon = icon.replace("star_unlist","star_list");
            $(this).children("img").attr("src",n_icon);
            $.get(link,{},function(){},"json");
        }
        return false;
    })
    /*Chọn hiển thị nhiều*/
    <?php }else if($this->cat->choice_multi){?>
    $(".choice-multi").click(function(){
        var icon = $(this).children("img").attr("src");
        var status = $(this).attr("rel");
        var link = $(this).attr("href");
        if(status==0){            
            var n_icon = icon.replace("star_unlist","star_list");
            $(this).attr("rel",1);
        }else{
            var n_icon = icon.replace("star_list","star_unlist");
            $(this).attr("rel",0);
        }
        $(this).children("img").attr("src",n_icon);
        $.get(link,{},function(){},"json");
        return false;
    })
    <?php }?>
    
    /*Xóa bài viết*/
    $(".del").click(function(){
        var _this = $(this);
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo</h5><p>Bạn có chắc muốn thực hiện hành động này không?</p></div>")
                            .dialog({            
            autoOpen: true,
            modal: true,
            buttons: {
                "Có": function(){                    
                    var link =  _this.attr("href");
                    _this.parent("td").parent("tr").remove();
                    $.get(link,{},function(){},"json");
                    $(this).dialog("close");
                },
                "Không": function(){
                    $(this).dialog("close");
                }
            }
        })
        return false;
    })
    
    /*Thay đổi thứ tự*/
    $(".change-local").change(function(){
        var link = $(this).attr("rel");
        var val = $(this).val();
        $.get(link,{"value":val},function(){});
    })
    
     /*Phân trang*/
    $(".pagination a").click(function(){
        var link = $(this).attr("href");
        $(".loading").show();
        $.get(link,null,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    /*Sắp xếp*/
    $(".sorting a").click(function(){
        var link = $(this).attr("href");
        var type = $(this).attr("rel");
        var filter_value = $(".searchkey").val();
        var sdata = {"sorttype":type,"filter_field":"article_name","filter_value":filter_value};
        if(filter_value.length==0){
            sdata={"sorttype":type};
        }
        $(".loading").show();
        $.post(link,sdata,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    <?php if($this->cat->has_album){?>
    /*Quản lý album ảnh*/
    $(".view-album").popupWindow({
        centerBrowser:1,
        width: 900,
        height: 500,
        scrollbars: 1
    });     
    <?php }?>
    
})
</script>